package com.mall.cart.controller;

import com.mall.cart.entity.dto.CartItemDTO;
import com.mall.cart.entity.vo.CartSummaryVO;
import com.mall.cart.service.CartService;
import com.mall.common.entity.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "购物车服务")
@RestController
@RequestMapping("/api/cart")
@RequiredArgsConstructor
public class CartController {

    private final CartService cartService;

    @Operation(summary = "加入商品到购物车")
    @PostMapping("/add")
    public Result<?> addItemToCart(@RequestBody CartItemDTO cartItemDTO) {
        cartService.addItemToCart(cartItemDTO);
        return Result.success();
    }

    @Operation(summary = "查看购物车中的商品")
    @GetMapping("/summary")
    public Result<CartSummaryVO> getCartSummary(@RequestParam Long userId) {
        CartSummaryVO cartSummaryVO = cartService.getCartSummary(userId);
        return Result.success(cartSummaryVO);
    }

    @Operation(summary = "修改购物车商品数量")
    @PutMapping("/quantity")
    public Result<?> updateCartItemQuantity(@RequestParam Long userId, @RequestParam Long productId, @RequestParam Integer quantity) {
        cartService.updateCartItemQuantity(userId, productId, quantity);
        return Result.success();
    }

    @Operation(summary = "删除购物车的商品")
    @DeleteMapping("/{productId}")
    public Result<?> deleteCartItem(@PathVariable Long productId) {
        cartService.removeById(productId);
        return Result.success();
    }

    @Operation(summary = "批量删除购物车的商品")
    @DeleteMapping("/{productId}")
    public Result<?> deleteCartItems(@PathVariable List<Long> productIds) {
        cartService.removeByIds(productIds);
        return Result.success();
    }
}